//
//  BKStatusRetweetView.swift
//  微博
//
//  Created by mac on 16/8/12.
//  Copyright © 2016年 xbk. All rights reserved.
//

import UIKit
import SnapKit

class BKStatusRetweetView: UIView {
    
    //记录转发微博的底部约束
    var retweetViewBottomConstraint : Constraint?
    
    //使用statusViewModel绑定数据
    var statusViewModel :BKStatusViewModel?{
    
        didSet {
            contentLabel.text = statusViewModel?.retweetContent
            
            //卸载上一次的约束
              retweetViewBottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls where picUrls.count > 0 {
            
                pictureView.hidden = false
                pictureView.picUrls = picUrls
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint =  make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
                
                
            }else{
            
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin).constraint
                })
            }
        }
    }

    //  MARK: -- 懒加载创建控件
    private lazy var contentLabel :UILabel = {
        let label  = UILabel(fontSize: 14, textColor: UIColor.grayColor())
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var pictureView :BKStatusPictureView = {
    
        let view = BKStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件设置约束
    private func setupUI () {
    
        addSubview(contentLabel)
        addSubview(pictureView)
        //pictureView.backgroundColor = RandomColor()
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contentLabel)
            //make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        
        //关键约束 作用:让view根据label变化.也就是当label变大View也会变大
        self.snp_makeConstraints { (make) -> Void in
            
           self.retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
    }
   


}
